﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BusinessLayer.FindArtInfo;
using BusinessLayer.FindArtInfo.Entities;
using BusinessLayer.FindArtInfo.Models;
using CommandLib;
using FindArtInfo.Common;
using FindArtInfo.Models;
using FindArtInfo.Common;

namespace FindArtInfo.Controllers
{
    public class ArtLinksController : MultiBase
    {
        //
        // GET: /ArtLinks/

        public ActionResult Search()
        {            
            PageArtLinkSearchModel model = new PageArtLinkSearchModel();
            model.ListCategory = new ArtLinksCategoryBL().GetAll();
            model.ListCountry = new CountryBL().GetAll();
            return View(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Search(PageArtLinkSearchModel model)
        {
            return
                Redirect(string.Format("~/{0}/ArtLinks/Result?keyWord={1}&cateId{2}&countryId={3}",
                                       Utils.GetUrlByLanguage(CookiesStore.GetCookies(Constants.Cookie.COOKIE_LANGUAGE)),
                                       model.KeyWord, model.CateId, model.CountryId));
        }

        public ActionResult Result(string keyWord, int? cateId, int? countryId)
        {
            List<ArtLinks> listArtLinks = new List<ArtLinks>();
            PageArtLinksSearchResultModel model = new PageArtLinksSearchResultModel();

            if (cateId != null)
            {
                ArtLinksCategory category = new ArtLinksCategoryBL().GetById(Convert.ToInt32(cateId));
                model.Cate = category != null
                                 ? category.Category
                                 : string.Empty;
            }

            if (countryId != null)
            {
                Country country = new CountryBL().GetById(Convert.ToInt32(countryId));
                model.Country = country != null ? country.CountryName : string.Empty;
            }

            if (string.IsNullOrEmpty(keyWord) && countryId == null && cateId != null)
            {
                listArtLinks = new ArtLinksBL().GetByCategory(Convert.ToInt32(cateId));
            }
            else
            {
                model.KeyWord = keyWord;
                listArtLinks = new ArtLinksBL().Search(keyWord, cateId, countryId);
            }
            model.ListArtLinks = listArtLinks;
            return View(model);
        }
    }
}
